<?php

    namespace Admin\Controller;

    use Common\Controller\AdminbaseController;

    class AdminRefundController extends AdminbaseController {

        protected $m_logistics;
        protected $m_logistics_bill;

        function _initialize() {
            parent::_initialize();
            $this->m_logistics= D("Admin/Logistics");
            $this->m_logistics_bill= D("Logistics/LogisticsBill");
            $this->m_user = D("Common/Users");
        }

        /**
         * 充值记录
         */
        public function index(){
            $where = array();
            $where['b.model'] = array('in',array('7','9'));
            
            $s_rid = I("request.s_rid");
            $this->assign("s_rid",$s_rid);
            if(!empty($s_rid)){
                $where['b.lid'] =$s_rid;
            }else{
                $lid = get_logistics_id();
                if($lid){
                    $lids = get_logistics_ids();
                    $where['b.lid'] = array('in',$lids);
                }
            }

            $admin_list = $this->m_user->alias('u')
            ->field('u.*, l.logistics_name')
            ->join('bdt_logistics l on l.id = u.organize_id','left')
            ->where(array('model'=>array('in','2,4,5,8,9,10,11,12,13')))
            ->select();
            $this->assign("admin_list",$admin_list);

            $s_admin_id = I("request.s_admin_id");
            $this->assign("s_admin_id",$s_admin_id);
            if(!empty($s_admin_id)){
                $where['b.uid'] = $s_admin_id;
            }

            $s_start_time = I("s_start_time");
            $s_end_time = I("s_end_time");
            if(empty($s_start_time)){
                $s_start_time = strtotime(date("Y-m-d 00:00:00")." -7 day");
            }else{
                $s_start_time = strtotime($s_start_time.":00");
            }
            if(empty($s_end_time)){
                $s_end_time = time();
            }else{
                $s_end_time = strtotime($s_end_time.":59");
            }
            $this->assign("s_start_time",$s_start_time);
            $this->assign("s_end_time",$s_end_time);
            $where["b.create_time"]=array("BETWEEN",array($s_start_time,$s_end_time));

            $count=$this->m_logistics_bill->where($where)->alias("b")->count();
            $page = $this->page($count, 15);

            if (I('s_download') != 1) {
                $list = $this->m_logistics_bill->get_list($where,"create_time DESC",$page->firstRow, $page->listRows);
            }else{
                $list = $this->m_logistics_bill->get_list($where,"create_time DESC",$page->firstRow,$count);
                $export_title = array(
                    'user_nicename' => '操作用户',
                    'user_phone' => '操作电话',
                    'logistics_name' => '物流公司名称',
                    'price' => '退款金额',
                    'balance' => '退款后余额',
                    'create_time' => '创建时间',
                );

                $excel = new \Common\Lib\excel();
                $excel->export_data('物流公司退款记录', $export_title, $list);
                exit;
            }

            $logistics_list = $this->m_logistics->get_list();
            $this->assign("logistics_list", $logistics_list);
            $this->assign("page", $page->show('Admin'));
            $this->assign("list",$list);
            $this->display();
        }


        /**
         * 退款
         */
        public function refund(){
            $id = I("get.id",0,'intval');
            if(!empty($id)){
                $item=$this->m_logistics->where(array('id'=>$id))->find();
                $this->assign("item",$item);
            }
            $this->display();
        }

        /**
         * 退款
         */
        public function refund_sub(){
            $id = I("get.id",0,'intval');
            if(!empty($id)){
                $item = M("Logistics")->where(array('id'=>$id))->find();
                $upper = M("Logistics")->where(array('id'=>$item['manager_id']))->find();

                $this->assign("item",$item);
                $this->assign("upper",$upper);
            }
            $this->display();
        }

        /**
         * 退款提交
         */
        public function refund_post(){
            if (IS_POST) {
                $id = I("post.id");
                $price = I("post.refund_price");
                
                if ($price <= 0) {
                    $this->error("请输入正确的退款金额");
                }

                $return = D("Logistics/LogisticsBill")->insert_data($id,$price,7);
                if ($return > 0) {
                    $this->success("退款成功！");
                }elseif ($return == -1) {
                    $this->error("系统错误！异常操作");
                }elseif ($return == -2) {
                    $this->error("公司信息不正确，无法操作");
                }elseif ($return == -3) {
                    $this->error("账户余额不足以进行退款操作");
                }
            }
        }

        /**
         * 退款提交
         */
        public function refund_sub_post(){
            if (IS_POST) {
                $id = I("post.id");
                $price = I("post.refund_price");
                if ($price <= 0) {
                    $this->error("请输入正确的退款金额");
                }

                $return = D("Logistics/LogisticsBill")->insert_data($id,$price,9);
                if ($return > 0) {
                    $this->success("退款成功！");
                }elseif ($return == -1) {
                    $this->error("系统错误！异常操作");
                }elseif ($return == -2) {
                    $this->error("公司信息不正确，无法操作");
                }elseif ($return == -3) {
                    $this->error("账户余额不足以进行退款操作");
                }
            }
        }


    }